﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace Colorful.CMS.Core
{
    public static class APIHelper
    {
        /// <summary>
        /// 验证码签名是否正确
        /// </summary>
        /// <param name="request"></param>
        /// <param name="appSecret"></param>
        /// <returns></returns>
        public static bool CheckSignature(HttpRequest request, string appSecret)
        {
            var appKey = request.Get("appkey");
            var nonce = request.Get("nonce");
            var timestamp = request.Get("timestamp");
            var signature = request.Get("signature");
            if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(nonce) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(signature)
                || string.IsNullOrEmpty(appSecret))
            {
                return false;
            }
            var result = SecurityHelper.GetSignature(appSecret, $"appkey={appKey}", nonce, timestamp);
            return result.Equals(signature, StringComparison.CurrentCultureIgnoreCase);
        }
    }
}
